home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / pcscheme / geneva / sources.exe / SOURCES / C / AB.C next >
Encoding:
C/C++ Source or Header  |  1993-10-24  |  4.7 KB  |  200 lines

  1. /* AB.C
  2.  ************************************************************************
  3.  *                                    *
  4.  *        PC Scheme/Geneva 4.00 Borland C code            *
  5.  *                                    *
  6.  * (c) 1985-1988 by Texas Instruments, Inc. See COPYRIGHT.TXT        *
  7.  * (c) 1992 by L. Bartholdi & M. Vuilleumier, University of Geneva    *
  8.  *                                    *
  9.  *----------------------------------------------------------------------*
  10.  *                                    *
  11.  *        Assembly Beautifier                    *
  12.  *                                    *
  13.  *----------------------------------------------------------------------*
  14.  *                                    *
  15.  * Created by: L. Bartholdi        Date: 1991            *
  16.  * Revision history:                            *
  17.  * - 18 Jun 92:    Renaissance (Borland Compilers, ...)            *
  18.  *                                    *
  19.  *                    ``In nomine omnipotentii dei''    *
  20.  ************************************************************************/
  21.  
  22. #include    <stdio.h>
  23. #include    <ctype.h>
  24. #include    <string.h>
  25.  
  26. char    line[257], new[257], backup;
  27.  
  28. char keywords[] = " proc segment ends endp macro endm extrn public ifdef "
  29.      "if ifb ifdif ife ifidn ifnb ifndef include label db dw dd equ "
  30.                     "page model dosseg radix stack title subttl struc union "
  31.      "comment assume group end .data .code .xcode .data? .stack "
  32.      "else endif org ";
  33.  
  34. char       *mystrtok( char *s )
  35. {
  36.     static char    b[257], *src;
  37.     char   *p;
  38.  
  39.     if( s )
  40.     {
  41.         src = s;
  42.         strcpy( b, s );
  43.     }
  44.     p = strtok( s, " \t");
  45.     if( p && *p != ',')
  46.     {
  47.         p[strlen(p)] = b[p-src+strlen(p)];
  48.         p = strtok( p, " \t,");
  49.     }
  50.     backup = b[p-src+strlen(p)];
  51.     return p;
  52. }
  53.  
  54. void    parse( char *in, char *out )
  55. {
  56.     static  comment = 0;
  57.     int        col = 0, len;
  58.                 char    cds[257];
  59.  
  60.     if( comment )
  61.     {
  62.         while( *in && *in != comment )
  63.             *out++ = *in++;
  64.         *out++ = *in;
  65.         if( *in == comment )
  66.             comment = 0;
  67.         in++;
  68.     } else
  69.                 if( isspace(*in) )
  70.                 {
  71.         *out++ = '\t';
  72.            col = 8;
  73.     }
  74.  
  75.     in = mystrtok( in );
  76.     
  77.     do {
  78. top:
  79.         len = strlen(in);
  80.  
  81.         if( *in == '\'')
  82.               {
  83.             in[len] = backup;
  84.             do {
  85.                 *out++ = *in++;
  86.                 col++;
  87.             } while( *in != '\'' && *in );
  88.             *out++ = *in++;
  89.             col++;
  90.             if( *in == ',')
  91.             {
  92.                 while( *in == ',')
  93.                                   {
  94.                     *out++ = *in++;
  95.                     col++;
  96.                 }
  97.                 *out++ = ' ';
  98.                 col++;
  99.                         }
  100.  
  101.             if( mystrtok( in ) == NULL )
  102.                 break;
  103.             else goto top;
  104.            } else 
  105.         if( *in == '"')
  106.         {
  107.             in[len] = backup;
  108.             do {
  109.                 *out++ = *in++;
  110.                 col++;
  111.             } while( *in != '"' && *in );
  112.             *out++ = *in++;
  113.             col++;
  114.             if( *in == ',')
  115.             {
  116.                 while( *in == ',')
  117.                                   {
  118.                     *out++ = *in++;
  119.                     col++;
  120.                 }
  121.                 *out++ = ' ';
  122.                 col++;
  123.             }
  124.             if( mystrtok( in ) == NULL )
  125.                 break;
  126.             else goto top;
  127.               }
  128.  
  129.         len = strlen(in);
  130.  
  131.         if( *in == ';')
  132.         {
  133.             in[len] = backup;
  134.             if( col )
  135.             while( col < 40 )
  136.             {
  137.                 *out++ = '\t';
  138.                 col += 8;
  139.             }
  140.                                                 strcpy( out, in );
  141.             return;
  142.         }
  143.  
  144.                                 strcpy( cds, in );
  145.            if( strcmp( strupr(cds), in ) != 0 )
  146.             strlwr( in );
  147.               sprintf( cds, " %s ", in );
  148.                  if( strstr( keywords, cds ) )
  149.             strupr( in );
  150.             
  151.         if( strcmp( in, "COMMENT") == 0 )
  152.            {
  153.             strcpy( out, in );
  154.                out += strlen( out );
  155.                                                 *out++ = ' ';
  156.             in = mystrtok( NULL );
  157.             in[strlen(in)] = backup;
  158.             comment = *in;
  159.                      strcpy( out, in );
  160.             return;
  161.         }
  162.  
  163.         strcpy( out, in );
  164.         col += strlen( in );
  165.         out += strlen( in );
  166.         if( out[-1] == ':' && col < 16 )
  167.         {
  168.             *out++ = '\n';
  169.             col = 0;
  170.         }
  171.             
  172.         if( backup == ',')
  173.         {
  174.                      *out++ = ',';
  175.             col++;
  176.         }
  177.  
  178.         if( out[-1] == ',' || col >= 15 || col == 8 || col == 9 )
  179.            {
  180.             *out++ = ' ';
  181.             col++;
  182.         } else {
  183.             *out++ = '\t';
  184.             col += 1 + ((255 - col) & 7);
  185.         }
  186.     } while( (in = mystrtok( NULL )) != NULL );
  187.     *out = 0;
  188. }
  189.  
  190. int     main()
  191. {
  192.     while( gets( line ) != NULL )
  193.                 {
  194.                                 parse( line, new );
  195.                  puts( new );
  196.     }
  197.     return 0;
  198. }
  199.  
  200.